package com.lz.sports.bean

import android.os.Parcel
import android.os.Parcelable
import android.util.ArrayMap
import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.Ignore
import androidx.room.PrimaryKey
import com.lz.library.extension.APP
import com.lz.library.proguard.IProguardField
import com.lz.sports.R

//检录组
@Entity(tableName = "ExamInspectGroup")
class ExamInspectGroup() : ParcelableString, IProguardField {

    @PrimaryKey(autoGenerate = true)
    @ColumnInfo(name = "examInspectGroupId")
    var id: Int = 0

    //检录设备组ID
    @ColumnInfo(name = "deviceGroupId")
    var deviceGroupId: Int = 0

    //考试计划ID
    @ColumnInfo(name = "examPlanId")
    var examPlanId: Int = 0

    //所属项目ID
    @ColumnInfo(name = "projectId")
    var projectId: Int = 0

    @ColumnInfo(name = "projectName")
    var projectName: String = ""

    //检录考区ID
    @ColumnInfo(name = "examAreaId")
    var examAreaId: Int = 0

    //检录的考点ID
    @ColumnInfo(name = "examSiteId")
    var examSiteId: Int = 0

    //检录时间
    @ColumnInfo(name = "inspectTime")
    var inspectTime: Long = 0L

    //考试时间
    @ColumnInfo(name = "examTime")
    var examTime: Long = 0L

    //检录时的校验方式(指纹, 人脸...)
    @ColumnInfo(name = "verifyType")
    var verifyType: Int = 0

    //是否上传？
    @ColumnInfo(name = "uploadStatus")
    var uploadStatus: Int = -1

    //照片上传状态
    @ColumnInfo(name = "photoUploadStatus")
    var photoUploadStatus: Int = -1

    //考试时的第几组名字
    @Ignore
    var examName: String = ""

    //是否考试过了(临时的考试, 可重复考多次考没有保存的情况下)
    @Ignore
    var hasTmpExamined: Boolean = false

    //该组下的所有检录信息
    @Ignore
    private val examInspectList = ArrayList<ExamInspect>()

    //以List方式暴露
    @Ignore
    val inspectList: List<ExamInspect> = examInspectList

    //检录索引, Number
    @Ignore
    private val inspectMap = ArrayMap<Int, ExamInspect>()

    //是否是相同的设备组
    fun isSameDeviceGroup(group: ExamInspectGroup): Boolean {
        return group.deviceGroupId == this.deviceGroupId
    }

    //通过序号查找检录信息
    fun findInspectExamineeByNumber(number: Int): ExamInspect? = inspectMap[number]

    //保存考试成绩
    fun saveExamineeResult() {
        examTime = System.currentTimeMillis()
        inspectList.forEach { it.saveResult(examTime) }
    }

    fun initInfo(inspectList: List<ExamInspectWithInfo>) {
        examInspectList.clear()
        inspectMap.clear()
        inspectList.forEach {
            if (!it.isInvalid()) {
                val inspect = it.examInspect!!.apply { examinee = it.examinee }
                examInspectList.add(inspect)
                inspectMap[it.examInspect!!.number] = inspect
            }
        }
    }

    fun hasExamined(): Boolean = examTime > 0

    //是否可以重考
    fun shouldReExaminee(): Boolean = !hasExamined() && hasTmpExamined

    //是否可以考试
    fun shouldExaminee(): Boolean = !hasExamined() && !hasTmpExamined

    @Ignore
    constructor(
        deviceGroupId: Int,
        examPlanId: Int,
        projectId: Int,
        projectName: String,
        examAreaId: Int,
        examSiteId: Int
    ) : this() {
        this.deviceGroupId = deviceGroupId
        this.examPlanId = examPlanId
        this.projectId = projectId
        this.projectName = projectName
        this.examAreaId = examAreaId
        this.examSiteId = examSiteId
        this.inspectTime = System.currentTimeMillis()
    }

    @Suppress("DEPRECATION")
    @Ignore
    constructor(parcel: Parcel) : this() {
        id = parcel.readInt()
        deviceGroupId = parcel.readInt()
        examPlanId = parcel.readInt()
        projectId = parcel.readInt()
        projectName = parcel.readString()!!
        examAreaId = parcel.readInt()
        examSiteId = parcel.readInt()
        inspectTime = parcel.readLong()
        examTime = parcel.readLong()
        verifyType = parcel.readInt()
        uploadStatus = parcel.readInt()
        photoUploadStatus = parcel.readInt()
        parcel.readList(inspectList, javaClass.classLoader)
        parcel.readMap(inspectMap, javaClass.classLoader)
    }

    override fun writeToParcel(parcel: Parcel, flags: Int) {
        parcel.writeInt(id)
        parcel.writeInt(deviceGroupId)
        parcel.writeInt(examPlanId)
        parcel.writeInt(projectId)
        parcel.writeString(projectName)
        parcel.writeInt(examAreaId)
        parcel.writeInt(examSiteId)
        parcel.writeLong(inspectTime)
        parcel.writeLong(examTime)
        parcel.writeInt(verifyType)
        parcel.writeInt(uploadStatus)
        parcel.writeInt(photoUploadStatus)
        parcel.writeList(inspectList)
        parcel.writeMap(inspectMap)
    }

    override fun str(): String = if (hasExamined()) APP.resources.getString(R.string.exam_has, examName) else examName

    override fun equals(other: Any?): Boolean = other === this ||
            (other is ExamInspectGroup && other.deviceGroupId == deviceGroupId && other.id == id)

    override fun describeContents(): Int {
        return 0
    }

    companion object CREATOR : Parcelable.Creator<ExamInspectGroup> {
        override fun createFromParcel(parcel: Parcel): ExamInspectGroup {
            return ExamInspectGroup(parcel)
        }

        override fun newArray(size: Int): Array<ExamInspectGroup?> {
            return arrayOfNulls(size)
        }
    }
}